<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 筛选区域 -->
         <SearchForm
      :searchData="pagination"
      @handleClear="handleClear"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
    ></SearchForm>
    <!-- 表格区域 -->
     <TableList
     @handleEdit="handleEdit"
     @handleBulid="handleBulid"
     @handleClickDelete="handleClickDelete"
     @handleForbidden="handleForbidden"
     @getCurrent="getCurrent"
     :total="total"
     :listData="listData"
     :pagination="pagination"
     ></TableList>
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
     <!-- 弹框 -->
      <DialogFrom 
      ref="formRef"
      :visible="visible"
      :title="title"
      :data="formData"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEditForm="handleEditForm" 
      :planData="planData" 
      ></DialogFrom>

  </div>
</template>

<script setup lang="ts">
      import SearchForm from './components/SearchForm.vue'
      import TableList from './components/TableList.vue'
      import DialogFrom from './components/DialogFrom.vue'
     
      import { ref,onMounted } from 'vue'
      import { MessagePlugin,DialogPlugin } from 'tdesign-vue-next'
      import { getLevelAllList,getLevelList,getLevelDetails,levelAdd,
        levelUpdate,levelDelete,levelStatus,getnursAllPlanList
       } from '@/api/serve'

      const visible = ref(false) // 新增，编辑弹窗
      const dataLoading = ref(false) // 加载中
      const formData = ref({}) // 弹窗表单内容
      const title = ref('') // 弹窗标题
      const total = ref(0) // 总条数
      const formRef = ref(null)
      const listData=ref([])
      const planData=ref([])
      const pagination = ref({
        pageNum: 1,
        pageSize: 5,
        total: 0,
      })
     
      const listAll=async ()=>{ 
        const  res= await getLevelList(pagination.value)
        listData.value=res.data.records;
         total.value=res.data.total
      }
      const getCurrent=async(pageInfo)=>{
        pagination.value.pageNum=pageInfo.current;
        pagination.value.pageSize=pageInfo.pageSize;
        listAll();
      }
      const planDataSeach=async()=>{
        const res=await getnursAllPlanList();
        planData.value=res.data;
      }
      //搜索
      const handleSearch=async()=>{ 
         listAll();
      }
      //编辑
      //回显
      const handleEdit=async(row)=>{
        visible.value=true;
        title.value="编辑";
        const res=await getLevelDetails(row.id);
           console.log(res); 
        console.log(res.data); 
        formData.value=res.data;
      }
      //清空
      const handleClear=async(val)=>{
            if(val=="name"){
               pagination.value.name="";
            }else{
               pagination.value.status="";
            }
            listAll();
      }

      //重置
      const handleReset=async()=>{
            pagination.value.name="";
            pagination.value.status="";
            listAll();
      }
      // 新增
      const handleBulid=async()=>{
        visible.value=true;
        title.value="新增";
        formBaseData.value={};
      }
      const handleClose=()=>{
        visible.value=false;
      }
     const handleAdd=async(formData)=>{
         const res=await levelAdd(formData);
         if(res.code==200){
            listAll();
            visible.value=false;
            formRef.value.handleClear();
          MessagePlugin.success('新增成功');  
         }else{
          MessagePlugin.error("新增失败");
         }
     }
     
     const handleEditForm=async(formData)=>{
         const res=await levelUpdate(formData);
         if(res.code==200){
            listAll();
            visible.value=false;
            formRef.value.handleClear();
          MessagePlugin.success('编辑成功');
         }else{
          MessagePlugin.error("编辑失败");
         }
        }



 const handleClickDelete=async(row)=>{
     const confirmDialog = DialogPlugin.confirm({
      header: '确定要删除吗',
      body: '是否确认删除？',
      confirmBtn: {
      content: '确定',
      theme: 'primary',
      loading: false,
    },
    theme: 'warning',
    onConfirm: async () => {
      try {
        // 更新按钮状态为加载中
        confirmDialog.update({
          confirmBtn: {
            content: '删除中',
            loading: true,
          },
        });

        // 模拟删除操作
        console.log(row.id);
        const res=await levelDelete(row.id);
             console.log(res);    
        // 检查删除结果
        if (res.code === 200) {
          MessagePlugin.success('删除成功');
          listAll();
        } else {
          MessagePlugin.error('删除失败');
        }
      } catch (error) {
        console.error('删除过程中发生错误:', error);
        MessagePlugin.error('删除失败');
      } finally {
        // 无论成功或失败，都隐藏对话框并重置按钮状态
        confirmDialog.update({
          confirmBtn: {
            content: '确定',
            loading: false,
          },
        });
        confirmDialog.hide();
      }
    },
  });
 }
  const disable=ref("禁用");
  const handleForbidden = async (row) => {
  const isEnabled = row.status === 0;
  const action = isEnabled ? "启用" : "禁用";
  disable.value = action;
  const confirmDialog = DialogPlugin.confirm({
    header: `确定要${action}吗`,
    body: `是否确认${action}？`,
    confirmBtn: {
      content: '确定',
      theme: 'primary',
      loading: false,
    },
    theme: 'warning',
    onConfirm: async () => {
      try {
        // 更新按钮状态为加载中
        confirmDialog.update({
          confirmBtn: {
            content: `${action}中`,
            loading: true,
          },
        });

        const res = await levelStatus(row);
        if (res.code === 200) {
          listAll();
          MessagePlugin.success(`${action}成功`);
        } else {
          MessagePlugin.error('操作失败');
        }
      } catch (error) {
        console.error(`${action}过程中发生错误:`, error);
        MessagePlugin.error(`${action}失败`);
      } finally {
        // 无论成功或失败，都隐藏对话框并重置按钮状态
        confirmDialog.update({
          confirmBtn: {
            content: '确定',
            loading: false,
          },
        });
        confirmDialog.hide();
      }
    },
  });
};
      onMounted(()=>{
        listAll();
        planDataSeach();
      })
     




</script>